package com.mine.frame.common.base

import android.content.Context
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ktx.immersionBar
import com.mine.frame.common.R
import com.mine.frame.common.bus.RxBus
import com.mine.frame.common.bus.RxBusBean
import com.mine.frame.common.utils.DisposableUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable

/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/11/11
 * Description:基类Activity
 */
abstract class BaseActivity<IV : IBaseView, P : BasePresenter<IV>> : AppCompatActivity(), IBaseView {

    /**
     * 当前presenter
     */
    protected lateinit var mPresenter: P

    /**eventBus*/
    private lateinit var busDisposable: Disposable

    /**
     * 当前界面的布局文件
     */
    @LayoutRes
    abstract fun initLayoutId(): Int


    /**
     * 初始化Presenter
     */
    abstract fun initPresenter(): P

    /**
     * 注册eventBus
     * 复写：true 注册事件
     *      false 不注册事件
     */
    protected open fun openRxBus() = false

    /**
     * 事件总线回调
     */
    protected open fun onReceiveEvent(bus: RxBusBean) {}

    /**
     * 状态栏样式设定
     */
    protected open fun initImmersionBar(){
        immersionBar {
            statusBarColor(R.color.status_bar_color)
            fitsSystemWindows(true)
        }

    }

    /**
     * 页面初始化之后执行的方法
     */
    protected abstract fun initLayoutAfter(savedInstanceState: Bundle?)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(initLayoutId())
        initImmersionBar()
        mPresenter = initPresenter()
        ARouter.getInstance().inject(this)
        initLayoutAfter(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        if (openRxBus()) {
            registerBus()
        }
    }

    override fun onStop() {
        super.onStop()
        unregisterBus()
    }


    override fun onDestroy() {
        DisposableUtils.instance.remove(getDisposableTag())
        if (this::mPresenter.isInitialized){
            mPresenter.onDestroyPresenter()
        }
        super.onDestroy()
    }

    /**
     * 注册eventBus
     */
    private fun registerBus(){
        busDisposable = RxBus.instance.toFlowable()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { bus ->
                onReceiveEvent(bus)
            }
    }

    /**
     * 注销eventBus
     */
    private fun unregisterBus(){
        if (this::busDisposable.isInitialized && !this.busDisposable.isDisposed){
            this.busDisposable.dispose()
        }
    }


    override fun getDisposableTag(): Any = javaClass.name

    override fun getContext(): Context = this

    override fun showInfo(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showErr(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showSuccess(str: String) {
        ToastUtils.showShort(str)
    }

    override fun showWrong(wrong: String) {
        ToastUtils.showShort(wrong)
    }

    override fun showToast(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showProgress(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun showProgressWithoutBg(msg: String) {
        ToastUtils.showShort(msg)
    }

    override fun hintProgress() {

    }


}